package com.shopmall.controller;

import com.shopmall.pojo.Admin;
import com.shopmall.pojo.AdminRecord;
import com.shopmall.pojo.Goods;
import com.shopmall.service.AdminRecordServiceImpl;
import com.shopmall.service.GoodsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    private GoodsServiceImpl goodsService;

    @Autowired
    private AdminRecordServiceImpl adminRecordService;

    @GetMapping("/goods/goods_show")
    public String goods_show(Model model){
        List<Goods> goods = goodsService.queryGoodsList();
        model.addAttribute("goods_s",goods);
        return "goods/goods_show";
    }

    @RequestMapping("/goods/add_goods")
    public String add_goods(){
        return "goods/add_goods";
    }

    @RequestMapping("/goods/add_goods_do")
    public String add_goods_do(Goods goods, Model model, HttpSession session){
        Timestamp date = new Timestamp(System.currentTimeMillis());
        Admin account = (Admin) session.getAttribute("admin");
        AdminRecord record = new AdminRecord();
        record.setDo_who(account.getUsername());
        record.setDo_time(date);
        int state = account.getState();
        String string1;
        if(state==0){
            string1="超级管理员";
        }
        else if(state==1){
            string1="系统管理员";
        }
        else {
            string1="商品管理员";
        }
        record.setDo_role(string1);
        record.setDone_who(""+goods.getGoods_id());
        record.setDone_role("商品");
        record.setDo_what("添加商品");
        //以上是记录信息设置

        Goods goods2 = goodsService.queryGoodsById(goods.getGoods_id());
        if(goods2!=null) {
            model.addAttribute("msg","已有相同编号的商品，添加失败！");
            return "home";
        }

        else {

            goods.setUpdate_date(date);
            goodsService.addGoods(goods);
            adminRecordService.addRecord(record);
            model.addAttribute("msg", "添加商品成功！");
            return "redirect:/goods/goods_show";

        }
    }

    @GetMapping("/goods/modify_goods/{id}")
    public String modify_goods(@PathVariable("id") int id,Model model){
        Goods goods = goodsService.queryGoodsById(id);
        model.addAttribute("goods",goods);
        return "goods/modify_goods";
    }

    @RequestMapping("/goods/modify_goods_do")
    public String modify_goods_do(Goods goods, Model model, HttpSession session){
        Timestamp date = new Timestamp(System.currentTimeMillis());
        Admin account = (Admin) session.getAttribute("admin");
        AdminRecord record = new AdminRecord();
        record.setDo_who(account.getUsername());
        record.setDo_time(date);
        int state = account.getState();
        String string1;
        if(state==0){
            string1="超级管理员";
        }
        else if(state==1){
            string1="系统管理员";
        }
        else {
            string1="商品管理员";
        }
        record.setDo_role(string1);
        record.setDone_who(""+goods.getGoods_id());
        record.setDone_role("商品");
        record.setDo_what("修改商品信息");
        //以上是记录信息设置


        goods.setUpdate_date(date);
        goodsService.updateGoods(goods);
        adminRecordService.addRecord(record);
        model.addAttribute("msg","成功修改编号为 "+goods.getGoods_id()+" 商品的信息！");
        return "redirect:/goods/goods_show";
    }

    @GetMapping("/goods/delete_goods/{id}")
    public String delete_goods(@PathVariable("id") int id, Model model, HttpSession session){
        Timestamp date = new Timestamp(System.currentTimeMillis());
        Admin account = (Admin) session.getAttribute("admin");
        AdminRecord record = new AdminRecord();
        record.setDo_who(account.getUsername());
        record.setDo_time(date);
        int state = account.getState();
        String string1;
        if(state==0){
            string1="超级管理员";
        }
        else if(state==1){
            string1="系统管理员";
        }
        else {
            string1="商品管理员";
        }
        record.setDo_role(string1);
        record.setDone_who(""+id);
        record.setDone_role("商品");
        record.setDo_what("删除商品");
        //以上是记录信息设置

        goodsService.deleteGoods(id);
        adminRecordService.addRecord(record);
        model.addAttribute("msg","成功删除编号为 "+id+" 的商品！");
        return "redirect:/goods/goods_show";
    }

    @RequestMapping("/goods/goods_in_one/{id}")
    public String goods_in_one(@PathVariable("id") int id, Model model){
        Goods goods = goodsService.queryGoodsById(id);
        model.addAttribute("goods",goods);
        return "/goods/goods_in_one";
    }

    /*
    @GetMapping("/queryGoodsByName/{name}")
    List<Goods> queryGoodsByName(@PathVariable("name") String name){
        return goodsService.queryGoodsByName(name);
    }

    @GetMapping("/queryGoodsByBrand/{brand}")
    List<Goods> queryGoodsByBrand(@PathVariable("brand") String brand){
        return goodsService.queryGoodsByBrand(brand);
    }

    @GetMapping("/queryGoodsById/{id}")
    Goods queryGoodsById(@PathVariable("id") int id){
        return goodsService.queryGoodsById(id);
    }

    @PostMapping("/addGoods")
    int addGoods(Goods goods){
        return goodsService.addGoods(goods);
    }

    @PostMapping("/updateGoods")
    int updateGoods(Goods goods){
        return goodsService.updateGoods(goods);
    }

   */


}
